/**
 * Created by gongxun on 17/1/13.
 */

var NnTableCardLayer = cc.Layer.extend({
    mBatchNode : null,
    mCardss : null,
    mSelectStatus : null,
    ctor : function(){
        this._super();

        this.mBatchNode = new cc.Node();
        this.addChild(this.mBatchNode);

        //牌
        this.mCardss = [];
        for (var i = 0; i < 5; i++){
            var cards = [];
            for(var j = 0; j < 5; j++){
                var card = new NnCard();
                cards.push(card);
                card.setVisible(false);
                this.mBatchNode.addChild(card);
            }
            this.mCardss.push(cards);
        }

        this.loadMyInhandCardAction()
    },

    //发牌动画
    setFapaiAnim : function(cardNumss){
        this.resetInhandCard();

        var index  = 0;
        var delayTime = 0;

        var bezierP2s = [
            cc.p(0, 100),
            cc.p(0, 50),
            cc.p(-100, 0),
            cc.p(100, 0),
            cc.p(0, 50)
        ];

        for (var seat = 0; seat < 5; seat++){
            var cardNums = cardNumss[seat];

            for(var i = 0; i < 5; i++){
                var card = this.mCardss[seat][i];
                if (cardNums.length == 0){
                    card.setVisible(false);
                    continue;
                }
                var cardNum = cardNums[i];

                var forScale = 0.3;
                var toScale = mo.nnCardHelper.getInhandCardScale(seat);
                var forPos = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 + 50 + index * 0.5);
                var toPos = mo.nnCardHelper.getInhandCardPos(seat, i);

                card.setPosition(forPos);
                card.setVisible(true);
                card.setCardNum(0);
                card.setScale(forScale);
                card.setTag(cardNum);

                //移动
                var controlPoints = [
                    forPos,
                    cc.p(toPos.x + bezierP2s[seat].x, toPos.y + bezierP2s[seat].y),
                    toPos
                ];
                var bezierTo1 = cc.bezierTo(0.5, controlPoints);

                //换牌
                var huanpai = null;
                if (cardNum != 0){
                    huanpai = cc.sequence(
                        cc.scaleTo(0.5 * 3 / 10, forScale + (toScale - forScale) * 4 / 10),
                        cc.scaleTo(0.5 * 2 / 10, 0, forScale + (toScale - forScale) * 6 / 10),
                        cc.callFunc(function () {
                            this.setCardNum(this.getTag());
                        }, card),
                        cc.scaleTo(0.5 * 5 / 10, toScale)
                    );
                }else{
                    huanpai = cc.sequence(
                        cc.scaleTo(0.5, toScale)
                    );
                }

                card.stopAllActions();
                card.runAction(cc.sequence(
                    cc.delayTime(delayTime),
                    cc.callFunc(function(){
                        mo.soundHelper.playNnFapai();
                    }, card),
                    cc.spawn(
                        bezierTo1,
                        huanpai
                    )
                ));

                index++;
                delayTime += 0.05;
            }
            delayTime += 0.05;
        }
    },

    //设置手牌
    setInhandCard : function(cardNumss, isAction){
        this.resetInhandCard();

        for (var seat = 0; seat < 5; seat++){
            var cardNums = cardNumss[seat];

            for(var i = 0; i < 5; i++){
                var card = this.mCardss[seat][i];
                if (cardNums.length == 0){
                    card.setVisible(false);
                    continue;
                }
                var cardNum = cardNums[i];

                var toScale = mo.nnCardHelper.getInhandCardScale(seat);
                var toPos = mo.nnCardHelper.getInhandCardPos(seat, i);

                var lastCardNum = card.mCardNum;

                card.stopAllActions();
                card.setPosition(toPos);
                card.setVisible(true);
                card.setCardNum(cardNum);
                card.setTag(cardNum);
                card.setScale(toScale);

                if (isAction != undefined && isAction && lastCardNum == 0){
                    if (cardNum != 0){
                        card.setCardNum(0);
                        card.runAction(cc.sequence(
                            cc.scaleTo(0.2, 0, toScale),
                            cc.callFunc(function(){
                                this.setCardNum(this.getTag());
                            }, card),
                            cc.scaleTo(0.2, toScale)
                        ));
                    }
                }
            }
        }
    },

    //开牌 -1不显示
    setkaiPaiAnim : function(cardTypes, cardNumss){
        for (var seat = 0; seat < 5; seat++){
            if (cardTypes[seat] != -1){
                var cards = this.mCardss[seat];
                var cardNums = cardNumss[seat];
                for (var i = 0; i < 5; i++){
                    var scale = mo.nnCardHelper.getShowCardScale(seat);
                    var pos = mo.nnCardHelper.getShowCardPos(seat, i, cardTypes[seat]);
                    cards[i].setScale(scale);
                    cards[i].setPosition(pos);
                    cards[i].setCardNum(cardNums[i]);
                }
            }
        }
    },

    //开牌
    setKaipaiOnePlayer : function(cardType, cardNums, seat, isAnim){
        if (cardType == -1){
            return;
        }

        var cards = this.mCardss[seat];
        for (var i = 0; i < 5; i++){
            var card = cards[i];
            var cardNum = cardNums[i];
            var scale = mo.nnCardHelper.getShowCardScale(seat);
            var pos = mo.nnCardHelper.getShowCardPos(seat, i, cardType);

            if (isAnim != undefined && isAnim){
                card.stopAllActions();
                var toScale = mo.nnCardHelper.getInhandCardScale(seat);
                var toPos = mo.nnCardHelper.getInhandCardPos(seat, i);
                card.setScale(toScale);
                card.setPosition(toPos);
                card.setCardNum(cardNum);

                card.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.2, pos),
                        cc.scaleTo(0.2, scale)
                    )
                ));
            }else{
                card.stopAllActions();
                card.setScale(scale);
                card.setPosition(pos);
                card.setCardNum(cardNums[i]);
            }
        }
    },

    //重置选中的手牌
    resetInhandCard : function(){
        this.mSelectStatus = [0, 0, 0, 0, 0];
        mo.nnTable.mNnTableUILayer.setSelectCardButtonStatus(0);

        var cards = this.mCardss[0];
        for (var i = 0; i < 5; i++){
            var card = cards[i];
            var pos = mo.nnCardHelper.getInhandCardPos(0, i);
            pos.y += (this.mSelectStatus[i] == 0 ? 0 : 30);
            card.setPosition(pos);
        }
    },

    //自己的手牌操作
    loadMyInhandCardAction : function(){
        var that = this;
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);

                var cards = that.mCardss[0];
                for (var i = 0; i < 5; i++){
                    var card = cards[i];
                    if (cc.rectContainsPoint(card.getBoundingBox(), point)){
                        that.setSelectInhandCard([i]);

                        //选牌音效
                        mo.soundHelper.playNnSelect(that.mSelectStatus[i] == 0);
                        break;
                    }
                }
                return false;
            },
            onTouchMoved: function(touch, event){
            },
            onTouchEnded: function(touch, event){
            },
            onTouchCancelled : function(touch, event){
            }
        }, this);
    },

    //尝试选中手牌
    setSelectInhandCard : function(selectIndexs){
        var myAction = mo.nnTableStatus.getMySelectCards();
        if (myAction == null){
            return;
        }

        //大的牛 不让选直接通知ui
        if (myAction.Rate == 1 ||
            myAction.Rate == 2 ||
            myAction.Rate == 3 ||
            myAction.Rate == 17 ||
            myAction.Rate == 18 ||
            myAction.Rate == 19 ||
            myAction.Rate == 20
        ){
            mo.nnTable.mNnTableUILayer.setSelectCardButtonStatus(1, myAction.Rate);
            return;
        }

        //小的牛
        var selectCardAddNum = 0;
        var selectCardCount = 0;
        var cards = this.mCardss[0];

        for (var i = 0; i < 5; i++){
            if (mo.gameHelper.findValueForArray(selectIndexs, i) != null){
                var value = this.mSelectStatus[i];
                if (value == 0){
                    value = 1
                }else{
                    value = 0;
                }
                if (selectIndexs.length > 1){
                    value = 1;
                }

                this.mSelectStatus[i] = value;
            }

            var card = cards[i];
            var pos = mo.nnCardHelper.getInhandCardPos(0, i);
            pos.y += (this.mSelectStatus[i] == 0 ? 0 : 30);
            card.setPosition(pos);
            if (this.mSelectStatus[i] == 1){
                selectCardAddNum += card.getCarcJisuanNum();
                selectCardCount++;
            }
        }

        if (selectCardCount == 3 && (selectCardAddNum % 10 == 0)){
            mo.nnTable.mNnTableUILayer.setSelectCardButtonStatus(1);
        }else{
            mo.nnTable.mNnTableUILayer.setSelectCardButtonStatus(0);
        }
    },

    //自动选中手牌选中状态
    updateSelectCardStatus : function(){
        var myAction = mo.nnTableStatus.getMySelectCards();
        if (myAction == null){
            return;
        }else{
            this.resetInhandCard();
        }

        var findNums = [myAction.Cards[0], myAction.Cards[1], myAction.Cards[2]];
        var array = [];
        for (var i = 0; i < 5; i++){
            array.push(this.mCardss[0][i].mCardNum);
        }

        var useIndexs = [];
        for (var i = 0; i < findNums.length; i++){
            var index = mo.gameHelper.findNumApartIndexs(array, findNums[i], useIndexs);
            if (index != null){
                useIndexs.push(index);
            }
        }

        this.setSelectInhandCard(useIndexs);
    }
});